<template>
  <div>
<van-datetime-picker
  v-model="currentDate"
  type="date"
  title="选择年月日"
  :min-date="minDate"
  :max-date="maxDate"
  @cancel="$emit('close')"
  @confirm='onConfirm'
/>
  </div>
</template>

<script>
import dayJs from 'dayjs'
import { editUserinfo } from '@/api/user.js'

export default {
  props: {
    value: {
      type: String,
      require: true
    }
  },
  data () {
    return {
      minDate: new Date('1900-01-01'),
      maxDate: new Date(),
      currentDate: new Date(2021, 12, 7)
    }
  },

  created () {
    this.currentDate = new Date(this.value)
  },

  methods: {
    async   onConfirm (val) {
      const formatTime = dayJs(val).format('YYYY-MM-DD')

      try {
        await editUserinfo({ birthday: formatTime })
        this.$toast.success('修改成功')
        this.$emit('close')
        this.$emit('input', formatTime)
      } catch (err) {
        console.log(err)
        this.$toast.fail('修改失败')
      }
    }
  }
}
</script>

<style scoped lang='less'>

</style>
